package com.sinog.front.controller.biz;

import com.sinog.core.model.UserDTO;
import com.sinog.core.util.ConvertHttpToMap;
import com.sinog.core.util.JsonUtil;
import com.sinog.core.utilbean.Constants;
import com.sinog.core.utilbean.Result;
import com.sinog.front.config.log.Log;
import com.sinog.front.feign.biz.PrisonerVisitService;
import com.sinog.front.service.user.UserFacade;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @desc 安置帮教管理 - 帮教对象信息管理 - 帮教对象走访管理 - controller
 * @author wangxj
 * @date 2022/3/9 10:59
 */
@Slf4j
@RestController
public class PrisonerVisitController {

    @Autowired
    private UserFacade userService;
    @Autowired
    private PrisonerVisitService prisonerVisitService;

    /**
     * 安置帮教管理 - 帮教对象信息管理 - 帮教对象走访管理 - 未走访列表
     * @param pageIndex pageIndex
     * @param pageSize pageSize
     * @param sortField 排序字段
     * @param sortOrder 排序方式
     * @param request request
     * @return String
     */
    @RequestMapping("/azbj/azbjgl/bjdxInfo/bjdxzf/list")
    public String getUnZfBaseInfoList(
            @RequestParam(required = false, defaultValue = "-1") Integer pageIndex,
            @RequestParam(required = false, defaultValue = "-1") Integer pageSize,
            @RequestParam(required = false) String sortField,
            @RequestParam(required = false) String sortOrder,HttpServletRequest request) {
        Map<String,Object> parameterMap = ConvertHttpToMap.getParameterMap(request);
        parameterMap.put("sortField", sortField);
        parameterMap.put("sortOrder", sortOrder);
        UserDTO user = userService.getAuthentication();
        String departId = user.getDepartid();
        int level = Integer.parseInt(user.getLevel());
        String orgid = user.getOrgid();
        return prisonerVisitService.getUnZfBaseInfoList(pageIndex,pageSize,parameterMap,departId,level,orgid);
    }

    /**
     * 安置帮教管理 - 帮教对象信息管理 - 帮教对象走访管理 - 已走访列表
     * @param pageIndex 服刑人员编号
     * @param pageSize pageSize
     * @param sortField 排序字段
     * @param sortOrder 排序方式
     * @param request request
     * @return String
     */
    @RequestMapping("/azbj/azbjgl/bjdxInfo/bjdxyzf/list")
    public String getZfBaseInfoList(
            @RequestParam(required = false, defaultValue = "-1") Integer pageIndex,
            @RequestParam(required = false, defaultValue = "-1") Integer pageSize,
            @RequestParam(required = false) String sortField,
            @RequestParam(required = false) String sortOrder,HttpServletRequest request) {
        Map<String,Object> parameterMap = ConvertHttpToMap.getParameterMap(request);
        parameterMap.put("sortField", sortField);
        parameterMap.put("sortOrder", sortOrder);
        UserDTO user = userService.getAuthentication();
        String departId = user.getDepartid();
        int level = Integer.parseInt(user.getLevel());
        String orgid = user.getOrgid();
        return prisonerVisitService.getZfBaseInfoList(pageIndex,pageSize,parameterMap,departId,level,orgid);
    }

    /**
     * 帮教工作管理 - 帮教对象走访管理 - 帮教对象走访管理 - 新增/编辑 - 走访保存
     * @param request request
     * @param data 业务数据
     * @return Result
     */
    @Log(type = "新增", value = "走访信息保存，参数中有id为编辑【更新】，没id为新增【新增】")
    @RequestMapping("/azbj/azbjgl/bjdxzf/saveBjZfInfo")
    public Result saveBjZfInfo(HttpServletRequest request,
                               @RequestParam(required = false, defaultValue = "-1") String data) {
        log.info("=============================saveBjZfInfo============================");
        UserDTO user = userService.getAuthentication();
        //取数据
        String edit = JsonUtil.getValueFromJsonstrByKey(data, "edit");
        Date date = new Date();
        if(Constants.StrFinalValue.TRUEVALUE.equals(edit)) {
            data = JsonUtil.setObjectToJsonStr(data, "updateUser", user.getUsername(), "updateTime", date);
        } else {
            data = JsonUtil.setObjectToJsonStr(data, "createUser", user.getUsername(),"updateUser",user.getUsername(),"createTime", date,"adduserid",user.getDepartid(),"uploadFlag",Constants.Number.NUM_ZERO,"updateTime",date);
        }
        return prisonerVisitService.saveBjZfInfo(data);
    }

    /**
     * 帮教工作管理 - 帮教对象走访管理 - 帮教对象走访管理 - 删除走访
     * @param zfId 走访编号
     * @return Result
     */
    @Log(type = "删除", value = "删除走访信息")
    @RequestMapping("/azbj/azbjgl/bjdxzf/view/delZfInfoAction")
    public Result delJzInfoAction(@RequestParam(required = false, defaultValue = "-1") String zfId) {
        return prisonerVisitService.delZfInfoAction(zfId);
    }

    /**
     * 安置帮教管理 - 帮教工作信息管理 - 帮教对象走访管理 - 走访 - 列表
     * @param priNum 服刑人员编号
     * @return List<Map<String,Object>>
     */
    @RequestMapping("/azbj/azbjgl/bjgzgl/view/getZfInfoList")
    public List<Map<String,Object>> getZfInfoList(@RequestParam(required = false, defaultValue = "-1") String priNum) {
        return prisonerVisitService.getZfInfoList(priNum);
    }
}